package mblog.web.controller.desk;

import mblog.core.data.Addresses;
import mblog.core.persist.service.AddressesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class AddressesController {

    @Autowired
    private AddressesService addressesService;

    @RequestMapping("ajaxAdd")
    @ResponseBody
    public Map<String,List<Addresses>> ajaxAdd(Integer otype,String parentId) {
        Map<String,List<Addresses>> dataMap = new HashMap<>();

        List<Addresses> dataList = new ArrayList<>();
        if(StringUtils.isNotEmpty(parentId)){
            dataList =  addressesService.findByParentId(parentId);
        }else{
            dataList = addressesService.findByParentId("1");
        }

        if(Objects.equals(otype,2)){
            dataMap.put("city",dataList);
            List<Addresses> locationList = addressesService.findByParentId(String.valueOf(dataList.get(0).getId()));
            dataMap.put("location",locationList);
        }else if(Objects.equals(otype,1)){
            dataMap.put("location",dataList);
        }
        return dataMap;
    }

}
